package cn.ac.jaas.isc.vegetable.pbms.controller;

import cn.ac.jaas.isc.vegetable.common.core.constant.CacheConstants;
import cn.ac.jaas.isc.vegetable.common.core.constant.PbmsCacheConstants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipmentType;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcEquipmentTypeService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 设备类型表
 *
 * @author 张高昌
 * @date 2022-05-14 16:34:43
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcequipmenttype")
@Api(value = "arcequipmenttype", tags = "设备类型表管理")
public class ArcEquipmentTypeController {

    private final ArcEquipmentTypeService arcEquipmentTypeService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param arcEquipmentType 设备类型表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_view')")
    public R getArcEquipmentTypePage(Page page, ArcEquipmentType arcEquipmentType) {
        return R.ok(arcEquipmentTypeService.page(page, Wrappers.query(arcEquipmentType)));
    }


    /**
     * 通过id查询设备类型表
     *
     * @param equipmentTypeId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{equipmentTypeId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_view')")
    public R getById(@PathVariable("equipmentTypeId") Integer equipmentTypeId) {
        return R.ok(arcEquipmentTypeService.getById(equipmentTypeId));
    }

    /**
     * 新增设备类型表
     *
     * @param arcEquipmentType 设备类型表
     * @return R
     */
    @ApiOperation(value = "新增设备类型表", notes = "新增设备类型表")
    @SysLog("新增设备类型表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_add')")
    @CacheEvict(value = PbmsCacheConstants.EQUIPMENT_TYPE, allEntries = true)
    public R save(@RequestBody @Valid ArcEquipmentType arcEquipmentType) {
        return arcEquipmentTypeService.saveEquipmentType(arcEquipmentType);
    }

    /**
     * 修改设备类型表
     *
     * @param arcEquipmentType 设备类型表
     * @return R
     */
    @ApiOperation(value = "修改设备类型表", notes = "修改设备类型表")
    @SysLog("修改设备类型表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_edit')")
    @CacheEvict(value = PbmsCacheConstants.EQUIPMENT_TYPE, allEntries = true)
    public R updateById(@RequestBody @Valid ArcEquipmentType arcEquipmentType) {
        if (arcEquipmentType.getEquipmentTypeId() == null) {
            return R.failed("设备类型id不能为空");
        }
        return arcEquipmentTypeService.updateEquipmentTypeById(arcEquipmentType);
    }

    /**
     * 通过id删除设备类型表
     *
     * @param equipmentTypeId id
     * @return R
     */
    @ApiOperation(value = "通过id删除设备类型表", notes = "通过id删除设备类型表")
    @SysLog("通过id删除设备类型表")
    @DeleteMapping("/{equipmentTypeId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_del')")
    @CacheEvict(value = PbmsCacheConstants.EQUIPMENT_TYPE, allEntries = true)
    public R removeById(@PathVariable Integer equipmentTypeId) {
        return arcEquipmentTypeService.removeEquipmentTypeById(equipmentTypeId);
    }


    /**
     * 导出excel 表格
     *
     * @param arcEquipmentType 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_export')")
    public List<ArcEquipmentType> export(ArcEquipmentType arcEquipmentType) {
        return arcEquipmentTypeService.list(Wrappers.query(arcEquipmentType));
    }

    @ApiOperation(value = "获取所有", notes = "获取所有")
    @GetMapping("/getEquipmentTypeAll")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipmenttype_view')")
    @Cacheable(value = PbmsCacheConstants.EQUIPMENT_TYPE, unless = "#result.data.isEmpty()")
    public R getEquipmentTypeAll() {
        return R.ok(arcEquipmentTypeService.list());
    }
}
